﻿using System;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Security.Cryptography;
using System.Text;
using System.Xml;

namespace _00_Work
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Program p = new Program();
            p.StartWeiXinPay("111", "2", "85452", "200");
            string b = MD5Encrypt("HEll000");
            Console.WriteLine(b);
            Console.ReadKey();
        }


        private void StartWeiXinPay(string productID, string ID, string secretCode, string price)
        {
            string kStrXML = "<xml></xml>";
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(kStrXML);
            XmlNode root = xmlDoc.SelectSingleNode("xml");

            string nowStr = DateTime.UtcNow.ToString();

            string pay_appid = "wx85032e7c98daea77";
            string pay_attach = secretCode;
            string pay_body = productID;//商品描述
            string pay_mch_id = "1497664882";//商户号
            string pay_nonce_str = nowStr;//随机字符串                       // NEED UPDATE
            string pay_notify_url = "/api/wechatonlinepay.php";//微信通知此连接支付结果，通知url必须为直接可访问的url，不能携带参数。示例：notify_url：“https://pay.weixin.qq.com/wxpay/pay.action”
            string pay_out_trade_no = nowStr;//商户订单号
            string pay_spbill_create_ip = getMyIPstr();//客户端ip地址
            string pay_total_fee = price;//总金额
            string pay_trade_type = "APP";
            string pay_key = "rzHUiCNx5oJGarYKAQdLN06H0gOqYcFR";//微信商户密钥设置


            XmlElement appid = xmlDoc.CreateElement("appid");
            XmlElement attach = xmlDoc.CreateElement("attach");
            XmlElement body = xmlDoc.CreateElement("body");
            XmlElement mch_id = xmlDoc.CreateElement("mch_id");
            XmlElement nonce_str = xmlDoc.CreateElement("nonce_str");
            XmlElement notify_url = xmlDoc.CreateElement("notify_url");
            XmlElement out_trade_no = xmlDoc.CreateElement("out_trade_no");
            XmlElement spbill_create_ip = xmlDoc.CreateElement("spbill_create_ip");
            XmlElement total_fee = xmlDoc.CreateElement("total_fee");
            XmlElement trade_type = xmlDoc.CreateElement("trade_type");
            XmlElement key = xmlDoc.CreateElement("key");
            XmlElement sign = xmlDoc.CreateElement("sign");

            appid.InnerText = pay_appid;
            attach.InnerText = pay_attach;
            body.InnerText = pay_body;
            mch_id.InnerText = pay_mch_id;
            nonce_str.InnerText = pay_nonce_str;
            notify_url.InnerText = pay_notify_url;
            out_trade_no.InnerText = pay_out_trade_no;
            spbill_create_ip.InnerText = pay_spbill_create_ip;
            total_fee.InnerText = pay_total_fee;
            trade_type.InnerText = pay_trade_type;
            key.InnerText = pay_key;

            root.AppendChild(appid);
            root.AppendChild(attach);
            root.AppendChild(body);
            root.AppendChild(mch_id);
            root.AppendChild(nonce_str);
            root.AppendChild(notify_url);
            root.AppendChild(out_trade_no);
            root.AppendChild(spbill_create_ip);
            root.AppendChild(total_fee);
            root.AppendChild(trade_type);
            root.AppendChild(key);

            string kStrMD5 = MD5Encrypt(xmlDoc.InnerXml);

            sign.InnerText = kStrMD5;
            root.AppendChild(sign);
            Console.WriteLine(xmlDoc.InnerXml);

        }

        private string getMyIPstr()
        {

            NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces(); ;
            foreach (NetworkInterface adapter in adapters)
            {
                if (adapter.Supports(NetworkInterfaceComponent.IPv4))
                {
                    UnicastIPAddressInformationCollection uniCast = adapter.GetIPProperties().UnicastAddresses;
                    if (uniCast.Count > 0)
                    {
                        foreach (UnicastIPAddressInformation uni in uniCast)
                        {
                            //得到IPv4的地址。 AddressFamily.InterNetwork指的是IPv4
                            if (uni.Address.AddressFamily == AddressFamily.InterNetwork)
                            {
                                return uni.Address.ToString();
                            }
                        }
                    }
                }
            }
            return "";
        }
        public static string MD5Encrypt(string strText)
        {
            //MD5 md5 = new MD5CryptoServiceProvider();
            //byte[] result = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(strText));
            //return System.Text.Encoding.Default.GetString(result);
            MD5 md5Hash = MD5.Create();
            //Convert the input string to a byte array and compute the hash.
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(strText));

            //Create a new StringBuilder to collect the bytes and create a string.
            StringBuilder builder = new StringBuilder();

            //Loop through each byte of the hashed data and format each one as a hexadecimal strings.
            for (int cnt = 0; cnt < data.Length; cnt++)
            {
                builder.Append(data[cnt].ToString("x2"));
            }
            return builder.ToString();

        }
    }
}
